unit Con_Nbm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Formulario_Padrao_Consulta, Grids, DBGrids, StdCtrls, Buttons, ExtCtrls,
  Db, Dbtables, TFlatButtonUnit, DBClient, Provider;

type
  TFrmCon_Nbm = class(TFrmConsulta)
    Label1: TLabel;
    EdtDesc: TEdit;
    DbgNbm: TDBGrid;
    bcadastrar: TSpeedButton;
    dsp: TDataSetProvider;
    CDS: TClientDataSet;
    edtDescricao: TEdit;
    Label2: TLabel;
    procedure BtnFecharClick(Sender: TObject);
    procedure EdtDescKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormActivate(Sender: TObject);
    procedure bcadastrarClick(Sender: TObject);
    procedure DbgNbmTitleClick(Column: TColumn);
    procedure edtDescricaoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DbgNbmDblClick(Sender: TObject);
  private
    { Private declarations }
    procedure FazConsulta(tp: byte);
  public
    { Public declarations }
    Nbm_codigo: string;
  end;

var
  FrmCon_Nbm: TFrmCon_Nbm;
  Novo: Boolean;

implementation

uses Dm, Biblio, clsNbm, Cad_Nbm;

{$R *.DFM}

var
  Nbm: TNbm;
  tds: Tdatasource;
  tq: Tquery;

procedure TFrmCon_Nbm.BtnFecharClick(Sender: TObject);
begin
  inherited;
  if (not Novo) or (Nbm_codigo = '') then
    Nbm_codigo := CDS.fieldbyname('Nbm_codigo').asstring;
  tds.free;
  tq.free;
end;

procedure TFrmCon_Nbm.EdtDescKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  inherited;
  if key = 12 then begin
    FazConsulta(1);
    EdtDesc.setfocus;
  end;
end;

procedure TFrmCon_Nbm.FormActivate(Sender: TObject);
begin
  inherited;
  Novo := False;
  tds := TDataSource.create(FrmCon_Nbm);
  tq := Tquery.create(FrmCon_Nbm);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  dsp.DataSet := tq;
  tds.DataSet := CDS;
  fazconsulta(1);
  EdtDesc.setfocus;
end;

procedure TFrmCon_Nbm.FazConsulta;
begin
  if EdtDesc.text = '' then
    EdtDesc.text := ' ';
  Nbm := TNbm.Create;
  if tp = 1 then
    Nbm.Consultar(tq, EdtDesc.text)
  else
    Nbm.Consultar(tq, ' ', EdtDescricao.text);
  CDS.cLOSE;
  CDS.open;
  Nbm.free;
  dbgNbm.DataSource := tds;
  dbgNbm.Refresh;
end;

procedure TFrmCon_Nbm.bcadastrarClick(Sender: TObject);
var
  f: TFrmCad_nbm;
begin
  inherited;
  Novo := True;
  f := TFrmCad_nbm.Create(Self);
  f.ShowModal;
  Nbm_codigo := f.xCodNBM;
  f.Free;
end;

procedure TFrmCon_Nbm.DbgNbmTitleClick(Column: TColumn);
begin
  inherited;
  CDS.IndexFieldNames := Column.FieldName;
end;

procedure TFrmCon_Nbm.edtDescricaoKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  inherited;
  if key = 12 then begin
    FazConsulta(2);
    EdtDescricao.setfocus;
  end;

end;

procedure TFrmCon_Nbm.DbgNbmDblClick(Sender: TObject);
begin
  inherited;
   BtnFecharClick(Sender);
end;

end.

